package com.jcgyl.export.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jcgyl.export.dao.TbreceiptDao;
import com.jcgyl.export.entity.OrderQueryModel;
import com.jcgyl.export.entity.Tborderhead;
import com.jcgyl.export.entity.Tborderlist;
import com.jcgyl.export.entity.Tbreceipt;
import com.jcgyl.export.model.BaseResponseModel;
import com.jcgyl.export.model.order.ReceiptModel;
import com.jcgyl.export.mq.MessageSender;
import com.jcgyl.export.service.OrderService;
import com.jcgyl.export.util.MessageHelper;
import com.jcgyl.export.util.entity.Result;

@Controller
@RequestMapping("/orderhead/")
public class OrderHeadController {

	@Autowired
	private TbreceiptDao receiptDao;
	@Autowired
	private OrderService orderService;
	@Autowired
	private MessageSender messageSender;

	/**
	 * 根据条件查询
	 * 
	 * @param orderModel
	 * @return JL
	 */
	@RequestMapping("doFindObject")
	@ResponseBody
	public Map<String, Object> doFindObject(@RequestBody OrderQueryModel orderModel) {
		// 获取前端传入的数据
		int pageSize = orderModel.getPageSize();
		int pageCount = orderModel.getPageIndex();
		String orderid = orderModel.getFilter().getOrderid();
		String batchnumber = orderModel.getFilter().getBatchnumber();
		String expressnumber = orderModel.getFilter().getExpressnumber();
		String starttime = orderModel.getFilter().getStart();
		String endtime = orderModel.getFilter().getEnd();
		Map<String, Object> map = orderService.findObject(orderid, batchnumber, expressnumber, starttime, endtime,
				pageSize, pageCount);
		return map;
	}

	// 查看回执信息
	@RequestMapping("doFindReceipt")
	@ResponseBody
	public Result doFindReceipt(@RequestBody ReceiptModel tbreceipt) {
		try {
			String receipttype = "IEPT302_BACK";
			String expressnumber = tbreceipt.getOrderid();
			System.out.println(expressnumber);
			Tbreceipt list = receiptDao.findReceiptContent(receipttype, expressnumber);
			if (list == null) {
				return Result.build(1, "查询失败");
			} else {
				return Result.build(0, "查询成功", list.getContent());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return Result.build(1, ex.getMessage());
		}
	}

	// 重推订单信息
	@PostMapping("/send")
	@ResponseBody
	public BaseResponseModel Send(@RequestBody String order_no) {
		BaseResponseModel model = new BaseResponseModel();
		try {
			// 查找订单
			Tborderhead order_head = orderService.findOrderheadByOrderId(order_no);
			List<Tborderlist> order_list = orderService.findOrderlistByOrderId(order_no);
			String order_msg = MessageHelper.GenerateOrderMessage(order_head, order_list);
			messageSender.send(order_msg);
		} catch (Exception e) {
			model.setIsError(true);
			model.setMessage(e.getMessage());
		}
		return model;
	}

	// 根据orderid来查询orderhead
	@PostMapping("/findOrderHeadByOrderId/{id}")
	@ResponseBody
	public Tborderhead findOrderHeadByOrderId(@PathVariable("id") String order_no) {
		return orderService.findOrderheadByOrderId(order_no);
	}

	// 根据orderid来查询orderlist
	@PostMapping("/findOrderListByOrderId/{id}")
	@ResponseBody
	public Map<String, Object> findOrderListByOrderId(@PathVariable("id") String order_no) {
		return orderService.findAllOrderlistByOrderId(order_no);
	}

	// 保存修改订单的表头信息
	@PostMapping("/save_orderhead")
	@ResponseBody
	public Result SaveOrderHead(@RequestBody Tborderhead orderhead) {
		try {
			orderService.updateOrderHead(orderhead);
			return Result.build(0, "修改orderhead成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(1, "修改ordehead失败！", e.getMessage());
		}

	}
	
	// 保存修改订单的表头信息
		@PostMapping("/save_orderlist")
		@ResponseBody
		public Result SaveOrderList(@RequestBody List<Tborderlist> list) {
			try {
				for(int i=0;i<list.size();i++){
					orderService.updateOrderList(list.get(i));
				}
				return Result.build(0, "修改orderlist成功！");
			} catch (Exception e) {
				e.printStackTrace();
				return Result.build(1, "修改orderlist失败！", e.getMessage());
			}

		}

}
